var config = {
	getUnselectedLangs : function() {
		var langs = localStorage["langs"];
		
		if (!langs)
			return getAllLanguages();
			 
		return JSON.parse(localStorage["langs"]); 
	},
	
	setUnselectedLangs: function(langs) {
		localStorage["langs"] = JSON.stringify(langs);
	},
	
	getSelectedLangs : function() {
		var langs = localStorage["selectedLangs"];
		
		if (!langs)
			return [];
			
		return JSON.parse(localStorage["selectedLangs"]); 
	},
	
	setSelectedLangs: function(langs) {
		localStorage["selectedLangs"] = JSON.stringify(langs);
	},
	
	getSourceLang : function() { return localStorage["sourceLang"]; },
	setSourceLang : function(source) { localStorage["sourceLang"] = source; },
	
	getTargetLang : function() { return localStorage["targetLang"];},
	setTargetLang : function(target) { localStorage["targetLang"] = target; },
	
	getLastQuery : function() { 
		var q = localStorage["lastQuery"];
		
		if (q == undefined)
			q = "";
			
		return q; 
	},
	
	setLastQuery : function(query) { localStorage["lastQuery"] = query; },
	
	getDefaultLang : function () { return localStorage["defaultLang"]; },
	setDefaultLang : function(lang) { localStorage["defaultLang"] = lang; },
	
	getAllLangs : function () { return getAllLanguages(); },
	
	isCtrlHotkey : function () {
		 var isCtrlShift = localStorage["isCtrlShiftHotkey"];
		 
		 if (!isCtrlShift || isCtrlShift == '0')
		 	return true;
			
		return false;
	},
	
	setIsCtrlHotkey : function (value) { localStorage["isCtrlShiftHotkey"] = value ? 0 : 1; },
	
	hotkeyCode : function () {
		var code = localStorage["hotkeyCode"];
		
		if (!code)
			return 0;
			 
		return parseInt(code); 
	},
	
	setHotkeyCode : function (value) { localStorage["hotkeyCode"] = value; },
	
	getHotkey : function () {
		var code = localStorage["hotkey"];
		
		if (!code)
			return '';
			 
		return code; 
	},
	
	setHotkey : function (value) { localStorage["hotkey"] = value; }
}

function translate(text, source, target, callback, saveQueryArgs) {

	if (saveQueryArgs)
		SaveQueryArgs(text, source, target);
		
	if (!source)
		source = "auto";
	
	var url = 'http://translate.google.com/translate_a/t?client=t&otf=1&pc=0&multires=1';
	url += '&text=' + text;
	url += '&sl=' + source;
	url += '&tl=' + target;

	var xhr = new XMLHttpRequest();
	xhr.open("GET", url, true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			var result = xhr.responseText;

			while(result.indexOf(",,") > 0) 
				result = result.replace(",,", ",[],");

			while(result.indexOf(",]") > 0)
				result = result.replace(",]", ",[]]");

			var obj = JSON.parse(result);
			
			if (isEmptyArray(obj) || isEmptyArray(obj[0]) || isEmptyArray(obj[0][0]) || obj[0][0][0] == text)
				callback(null, text);
			else
				callback(obj, text);
		}
	}
	xhr.send();
}

function renderTranslation(result) {
	if (!result)
		return null;
		
	var res = "<div class='main'>" + result[4][0][0] + "</div>";

	var list = result[1];

	var details = '';
	
	for (var i = 0; i < list.length; i++) {
		var data = list[i]

		details += "<h4>" + data[0] +"</h4>";
		details += "<ul>";
		for (var j = 0; j < data[1].length; j++) {
			details += "<li>" + data[1][j] + "</li>"
		}
		details += "</ul>";
	}

	if (details != '')
		res += '<div class="details">' + details + '</div>';
		
	return res;
}
		
function saveOptions(unselectedLangList, selectedLangList, defaultLang, isCtrlKey, hotkeyCode, hotkey) {
	config.setUnselectedLangs(unselectedLangList);
	config.setSelectedLangs(selectedLangList);
	config.setDefaultLang(defaultLang);
	config.setIsCtrlHotkey(isCtrlKey);
	config.setHotkeyCode(hotkeyCode);
	config.setHotkey(hotkey);
}

function isEmptyArray(value) {
	return !value.length;
}

function SaveQueryArgs(text, source, target) {
	config.setSourceLang(source);
	config.setTargetLang(target);
	config.setLastQuery(text);
}

function getAllLanguages() {
	return [
		{ value: 'af', text: 'Afrikaans' },
		{ value: 'sq', text: 'Albanian' },
		{ value: 'ar', text: 'Arabic' },
		{ value: 'be', text: 'Belarusian' },
		{ value: 'bg', text: 'Bulgarian' },
		{ value: 'ca', text: 'Catalan' },
		{ value: 'zh-CN', text: 'Chinese (Simplified)' },
		{ value: 'zh-TW', text: 'Chinese (Traditional)' },
		{ value: 'hr', text: 'Croatian' },
		{ value: 'cs', text: 'Czech' },
		{ value: 'da', text: 'Danish' },
		{ value: 'nl', text: 'Dutch' },
		{ value: 'en', text: 'English' },
		{ value: 'et', text: 'Estonian' },
		{ value: 'tl', text: 'Filipino' },
		{ value: 'fi', text: 'Finnish' },
		{ value: 'fr', text: 'French' },
		{ value: 'gl', text: 'Galician' },
		{ value: 'de', text: 'German' },
		{ value: 'el', text: 'Greek' },
		{ value: 'iw', text: 'Hebrew' },
		{ value: 'hi', text: 'Hindi' },
		{ value: 'hu', text: 'Hungarian' },
		{ value: 'is', text: 'Icelandic' },
		{ value: 'id', text: 'Indonesian' },
		{ value: 'ga', text: 'Irish' },
		{ value: 'it', text: 'Italian' },
		{ value: 'ja', text: 'Japanese' },
		{ value: 'ko', text: 'Korean' },
		{ value: 'lv', text: 'Latvian' },
		{ value: 'lt', text: 'Lithuanian' },
		{ value: 'mk', text: 'Macedonian' },
		{ value: 'ms', text: 'Malay' },
		{ value: 'mt', text: 'Maltese' },
		{ value: 'no', text: 'Norwegian' },
		{ value: 'fa', text: 'Persian' },
		{ value: 'pl', text: 'Polish' },
		{ value: 'pt', text: 'Portuguese' },
		{ value: 'ro', text: 'Romanian' },
		{ value: 'ru', text: 'Russian' },
		{ value: 'sr', text: 'Serbian' },
		{ value: 'sk', text: 'Slovak' },
		{ value: 'sl', text: 'Slovenian' },
		{ value: 'es', text: 'Spanish' },
		{ value: 'sw', text: 'Swahili' },
		{ value: 'sv', text: 'Swedish' },
		{ value: 'th', text: 'Thai' },
		{ value: 'tr', text: 'Turkish' },
		{ value: 'uk', text: 'Ukrainian' },
		{ value: 'vi', text: 'Vietnamese' },
		{ value: 'cy', text: 'Welsh' },
		{ value: 'yi', text: 'Yiddish' }
	];
}